package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssm.base.BaseController;
import com.ssm.entity.Admin;
import com.ssm.entity.HouseBroker;
import com.ssm.service.AdminService;
import com.ssm.service.HouseBrokerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/19
 */
@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController extends BaseController {
    @Reference
    private AdminService adminService;
    @Reference
    private HouseBrokerService houseBrokerService;

    /**
     * 进入添加
     * @param map
     * @param houseId
     * @return
     */
    @GetMapping("/create")
    public String create(Map map, @RequestParam("houseId") Long houseId) {
        List<Admin> adminList = adminService.findAll();
        map.put("adminList",adminList);
        map.put("houseId",houseId);
        return "houseBroker/create";
    }

    /**
     * 保存添加
     * @param houseBroker
     * @return
     */
    @PostMapping("/save")
    public String save(HouseBroker houseBroker) {
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        houseBrokerService.insert(houseBroker);
        return "common/successPage";
    }

    /**
     * 修改
     * @param map
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(Map map, @PathVariable Long id) {
        HouseBroker houseBroker = houseBrokerService.getById(id);
        List<Admin> adminList = adminService.findAll();
        map.put("adminList",adminList);
        map.put("houseBroker",houseBroker);
        return "houseBroker/edit";
    }

    /**
     * 保存修改
     * @param houseBroker
     * @return
     */
    @PostMapping("/update")
    public String update(HouseBroker houseBroker) {
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        houseBrokerService.update(houseBroker);
        return "common/successPage";
    }

    @GetMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable("houseId") Long houseId,@PathVariable("id") Long id) {
        houseBrokerService.deleteById(id);
        return "redirect:/house/"+houseId;
    }
}
